// b2c/order/order.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    wh:null,
    cookie:"",
    orderType:"",
    mate:[
      {
        title:"全部",
        orderType:""
      },
      {
        title:"待付款",
        orderType:"已提交"
      },
      {
        title:"待发货",
        orderType:"已受理"
      },
      {
        title:"已发货",
        orderType:"已发货"
      },
      {
        title:"已完成",
        orderType:"已完成"
      }
    ],
    page:1,
    num:8,
    mateArray:[],
    mateNum:0,
    newMateNum:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      orderType: options.itype
    }) 
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.setNavigationBarTitle({
      title: '订单列表'
    })
    
    try {
      var res = wx.getSystemInfoSync();
      this.setData({
        wh: res.windowHeight + 'px'
      })

    } catch (e) {
      // Do something when catch error
      console.log(e)
    }
    var self = this;
    var a = wx.getStorageSync('cookie') || [];
    this.setData({
      cookie: a,
      mateArray: [],
      page: 1
    })
    wx.request({
      url: "https://csb2c.fenxiangqu.cn/xcx_zb_000017x",
      data: {
        orderType: self.data.orderType,
        page: 1,
        num: self.data.num
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': "LID=" + this.data.cookie
      },
      method: "POST",
      success: (res) => {
        wx.hideLoading();
        if(res.data=="login"){
          wx.showToast({
            title: '请登录',
            icon: 'success',
            duration: 1000
          })
        }else{
          if (res.data[0].length) {
            for (var i = 0; i < res.data[0].length; i++) {
              var stri = self.data.mateArray;
              stri.push(res.data[0][i]);
              self.setData({
                mateArray: stri
              })
            }
          }
          self.setData({
            mateNum: res.data[1],
            newMateNum:res.data[0].length
          })
        }
                
        
      },
      fail: (res) => {
        wx.hideLoading();
        console.log(res)
      }
    })
  },
  orderTf:function(e){
    var self = this;
    this.setData({
      orderType: e.currentTarget.dataset.ty,
      mateArray:[],
      page: 1
    })
    wx.request({
      url: "https://csb2c.fenxiangqu.cn/xcx_zb_000017x",
      data: {
        orderType:self.data.orderType,
        page:1,
        num:self.data.num
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': "LID=" + this.data.cookie
      },
      method: "POST",
      success: (res) => {
        wx.hideLoading();
        if(res.data=="login"){
          wx.showToast({
            title: '请登录',
            icon: 'success',
            duration: 1000
          })
        }else{
          if (res.data[0].length) {
            for (var i = 0; i < res.data[0].length; i++) {
              var stri = self.data.mateArray;
              stri.push(res.data[0][i]);
              self.setData({
                mateArray: stri
              })
            }
          }
          self.setData({
            mateNum: res.data[1],
            newMateNum: res.data[0].length
          })
        }
      },
      fail: (res) => {
        wx.hideLoading();
        console.log(res)
      }
    })
  },
  Eventtop:function(e){  
    var self = this;
    if (this.data.mateNum > this.data.num){
      if (this.data.newMateNum >= this.data.num){
        
        wx.request({
          url: "https://csb2c.fenxiangqu.cn/xcx_zb_000017x",
          data: {
            orderType: self.data.orderType,
            page: self.data.page + 1,
            num: self.data.num
          },
          header: {
            'content-type': 'application/x-www-form-urlencoded',
            'Cookie': "LID=" + this.data.cookie
          },
          method: "POST",
          success: res => {
            if (res.data == "login") {
              wx.showToast({
                title: '请登录',
                icon: 'success',
                duration: 1000
              })
            } else {
              if (res.data[0].length) {
                for (var i = 0; i < res.data[0].length; i++) {
                  var stri = self.data.mateArray;
                  stri.push(res.data[0][i]);
                  self.setData({
                    mateArray: stri
                  })
                }
              }
              self.setData({
                mateNum: res.data[1],
                page: self.data.page + 1,
                newMateNum: res.data[0].length
              })
            }
          },
          fail: res => {
            wx.showToast({
              title: '网络故障',
              icon: 'success',
              duration: 1000
            })
          }
        })
      }
      
    }
  },
  eventComm:res =>{
      wx.navigateTo({
        url: '../ddbj/ddbj?orderId='+res.currentTarget.dataset.orderid,
      })
  },
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function (e) {
    
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})